package com.yyh.myblog.interceptor;

//import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author yyh
 * @date 2021/2/23 16:34
 * 拦截器，拦截后台页面，必须登录后才能访问后台页面
 */

public class LoginInterceptor extends HandlerInterceptorAdapter {

    // preHandle在方法调用前使用

    @Override
    public boolean preHandle(HttpServletRequest request,
                             HttpServletResponse response,
                             Object handler) throws Exception {
        if (request.getSession().getAttribute("user") == null) {
           //未登录重定向到登录页面
            response.sendRedirect("/admin");
            return false;
        }
        return true;
    }
}
